<?php

class Users extends CI_Controller {
    
    private $_auth;
    public $_format = "Y-m-d H:i:s";
    public function __construct() {
        parent::__construct();
        $this->_auth = $this->myclass->checkuser();
        if($this->_auth == FALSE) redirect ("backend/home");
    }
    
    public function index(){
        $u = $this->session->userdata('user');

        if ($this->input->post("info")) {
            $post = $this->input->post("data");

            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $this->form_validation->set_rules('data[name]', 'Tên', 'trim|required');
            $this->form_validation->set_rules('data[phone]', 'Số điện thoại', 'trim|required|integer|min_length[7]|max_length[13]');
            $this->form_validation->set_rules('data[address]', 'Địa chỉ', 'trim|required');
            if ($this->form_validation->run('info') == TRUE) {
                $this->musers->update($u['id'], $post);
                $data['data']['notice1'] = "Thay đổi thành công";
            }
        } elseif ($this->input->post("change-pass")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $this->form_validation->set_rules('oldpass', 'Mật khẩu cũ', 'trim|required|callback_info_pass[' . $u["email"] . ']');
            $this->form_validation->set_rules('data[pass]', 'Mật khẩu mới', 'trim|required|matches[passconf]');
            $this->form_validation->set_rules('passconf', 'Xác nhận mật khẩu', 'trim|required');
            if ($this->form_validation->run('change-pass') == TRUE) {
                $post['pass'] = do_hash($post['pass'], 'md5');
                $this->musers->update($u['id'], $post);
                $user = $this->musers->getCusByEmail($u["email"]);
                $this->session->set_userdata('user', $user);                
                $this->myclass->js_redirect("Đổi mật khẩu thành công", base_url("backend/users"));
            }
        }
       
        $data['data']['cus'] = $this->musers->fetchOne($u['id']);
        
        $data['title'] = "Quản lý thông tin";
        $data['temp'] = "backend/users/home";
        $this->load->view("backend/layout/index",$data);
    }
    
    public function info_pass($pass, $email) {
        $user = $this->musers->getCusByEmail($email);
        $pass = do_hash($pass, 'md5');
        if ($pass != $user['pass']) {
            $this->form_validation->set_message('info_pass', 'Mật khẩu không đúng');
            return FALSE;
        } else
            return TRUE;
    }
    
    public function create(){
        if ($this->input->post("register")) {
            $post = $this->input->post("data");

            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $this->form_validation->set_rules('data[email]', 'Email', 'trim|required|valid_email|is_unique[users.email]|xss_clean');           
            $this->form_validation->set_rules('data[pass]', 'Mật khẩu', 'trim|required|matches[passconf]');
            $this->form_validation->set_rules('passconf', 'Xác nhận mật khẩu', 'trim|required');
            if ($this->form_validation->run() == TRUE) {
                $post['pass'] = do_hash($post['pass'], 'md5');
                $post['created'] = gmdate($this->_format, time() + 7 * 3600);
                $this->musers->insert($post);
                $data['data']['notice'] = "Đăng ký thành công";
            }
        }
        $data['title'] = "Tạo tài khoản";
        $data['temp'] = "backend/users/form";
        $this->load->view("backend/layout/index",$data);        
    }
    
}
